package com.xiang._01;

import com.google.common.base.Splitter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字符串与Collection的互转
 */
public class Str2Collection {
    public static void main(String[] args) {


        List<String> strings = str2list2("a,b,c", ",");

        List<String> stringGuava = str2list3("a,b,c", ",");


    }

    //借助 Collections.addAll
    public static List<String> str2list2(String str, String split) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, str.split(split));
        return list;
    }


    // guava方式
    public static List<String> str2list3(String str, String split) {
        return Splitter.on(split).splitToList(str);
    }

    //Stream流处理
    public List<Integer> str2intListV2(String str, String split) {
        return Splitter.on(split).splitToStream(str)
                .map(String::trim).filter(s -> !s.isEmpty())
                .map(Integer::valueOf).collect(Collectors.toList());
    }
}
